<template>
  <div class="purchase-page">
    <div class="page-header">
      <h2>采购管理</h2>
      <el-button type="primary" @click="handleCreate">
        <el-icon><Plus /></el-icon>
        新建采购单
      </el-button>
    </div>
    
    <el-card>
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm">
          <el-form-item label="订单号">
            <el-input v-model="searchForm.orderNo" placeholder="请输入订单号" />
          </el-form-item>
          <el-form-item label="供应商">
            <el-input v-model="searchForm.supplier" placeholder="请输入供应商" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="searchForm.status" placeholder="请选择状态">
              <el-option label="全部" value="" />
              <el-option label="待审核" value="pending" />
              <el-option label="已审核" value="approved" />
              <el-option label="已完成" value="completed" />
              <el-option label="已取消" value="cancelled" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="orderNo" label="订单号" width="150" />
        <el-table-column prop="supplier" label="供应商" />
        <el-table-column prop="totalAmount" label="总金额" width="120" />
        <el-table-column prop="orderDate" label="订单日期" width="120" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleView(row)">
              查看
            </el-button>
            <el-button type="primary" link @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="danger" link @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'

const searchForm = reactive({
  orderNo: '',
  supplier: '',
  status: ''
})

const tableData = ref([
  {
    orderNo: 'PO20240101001',
    supplier: '供应商A有限公司',
    totalAmount: '¥5,200.00',
    orderDate: '2024-01-01',
    status: 'completed'
  },
  {
    orderNo: 'PO20240101002',
    supplier: '供应商B有限公司',
    totalAmount: '¥3,800.00',
    orderDate: '2024-01-02',
    status: 'approved'
  },
  {
    orderNo: 'PO20240101003',
    supplier: '供应商C有限公司',
    totalAmount: '¥7,500.00',
    orderDate: '2024-01-03',
    status: 'pending'
  }
])

const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

const getStatusType = (status) => {
  const types = {
    pending: 'warning',
    approved: 'primary',
    completed: 'success',
    cancelled: 'danger'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    pending: '待审核',
    approved: '已审核',
    completed: '已完成',
    cancelled: '已取消'
  }
  return texts[status] || status
}

const handleSearch = () => {
  // 搜索逻辑
}

const handleReset = () => {
  searchForm.orderNo = ''
  searchForm.supplier = ''
  searchForm.status = ''
}

const handleCreate = () => {
  // 创建采购单逻辑
}

const handleView = (row) => {
  // 查看详情逻辑
}

const handleEdit = (row) => {
  // 编辑逻辑
}

const handleDelete = (row) => {
  // 删除逻辑
}

const handleSizeChange = (val) => {
  pageSize.value = val
}

const handleCurrentChange = (val) => {
  currentPage.value = val
}
</script>

<style scoped>
.purchase-page {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #333;
}

.search-bar {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 4px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
